package com.example.tableapplication;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class TableRowView extends LinearLayout {

    public static final ArrayList<String> selectedStrings = new ArrayList<String>();    //用于存放为勾选状态的复选框的值

    public TableRowView(Context context, TableRow tableRow, final int odd) {
        super(context);
        this.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 0; i < tableRow.getSize(); i++) {//逐个格单元添加到行
            TableCell tableCell = tableRow.getCellValue(i);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    tableCell.width, tableCell.height);//按照格单元指定的大小设置空间
            layoutParams.setMargins(1, 1, 1, 1);//预留空隙制造边框
            if (tableCell.type == TableCell.STRING) {//如果格单元是文本内容
                TextView textCell = new TextView(context);
                textCell.setLines(1);
                textCell.setGravity(Gravity.CENTER);
                double s=(odd-MainActivity_2.HeadTitle.length-1) % 8;
                if(odd==0||((odd-MainActivity_2.headlen) % MainActivity_2.bodylen == 0 &&(odd-MainActivity_2.headlen) >= 0))
                    textCell.setBackgroundColor(Color.GREEN);//背景   灰色
                else
                    textCell.setBackgroundColor(Color.WHITE);//背景   白色
                textCell.setText(String.valueOf(tableCell.value));
                addView(textCell, layoutParams);
            } else if (tableCell.type == TableCell.IMAGE) {//如果格单元是图像内容
                ImageView imgCell = new ImageView(context);
                imgCell.setBackgroundColor(Color.WHITE);//背景灰色
                imgCell.setImageResource((Integer) tableCell.value);
                addView(imgCell, layoutParams);
            }else if (tableCell.type == TableCell.CHECKBOX){       //如果单元格类型为控件（复选框）
                final CheckBox checkBox=new CheckBox(context);
                checkBox.setText(String.valueOf(tableCell.value));
                checkBox.setBackgroundColor(Color.GREEN);
                int sd=((odd-MainActivity_2.headlen)/MainActivity_2.bodylen)+1;
                if (odd != 0) {
                    checkBox.setEnabled(TableAdapter.checkBoxes.get(((odd-MainActivity_2.headlen)/MainActivity_2.bodylen)+1).isEnabled());
                    checkBox.setChecked(TableAdapter.checkBoxes.get(((odd-MainActivity_2.headlen)/MainActivity_2.bodylen)+1).isChecked());    //设置checkbox的勾选状态为存储到TableAdapter中的相应位置的列表中的复选框的勾选状态
                }else if(odd==0){
                    checkBox.setEnabled(TableAdapter.checkBoxes.get(0).isEnabled());
                    checkBox.setChecked(TableAdapter.checkBoxes.get(0).isChecked());
                }
                addView(checkBox, layoutParams);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {    //checkedbox的事件监听中同时改变在TableAdapter的列表中相应位置的复选框的勾选状态；
                            if (odd != 0) {
                                selectedStrings.add(checkBox.getText().toString());
                                TableAdapter.checkBoxes.get(((odd-MainActivity_2.headlen)/MainActivity_2.bodylen)+1).setChecked(true);
                            }else if(odd==0){
                                selectedStrings.clear();
                                selectedStrings.add(checkBox.getText().toString());
                                TableAdapter.checkBoxes.get(0).setChecked(true);
                                for (int i = 1; i <TableAdapter.checkBoxes.size(); i++) {      //复选框的个数为：count=行总数/8，创建count个复选框设置初始勾选状态为false
                                    TableAdapter.checkBoxes.get(i).setChecked(false);        //相当于将表格中的checkbox的勾选状态存储到该列表中的相应位置，然后在需要的时候再取出来。
                                    TableAdapter.checkBoxes.get(i).setEnabled(false);
                                }
                                MainActivity_2.tableAdapter.notifyDataSetChanged();
                            }

                        }else{
                            if (odd != 0) {
                                selectedStrings.remove(checkBox.getText().toString());
                                TableAdapter.checkBoxes.get(((odd-MainActivity_2.headlen)/MainActivity_2.bodylen)+1).setChecked(false);
                            }else if(odd==0){
                                selectedStrings.remove(checkBox.getText().toString());
                                TableAdapter.checkBoxes.get(0).setChecked(false);
                                for (int i = 1; i <TableAdapter.checkBoxes.size(); i++) {      //复选框的个数为：count=行总数/8，创建count个复选框设置初始勾选状态为false
                                    TableAdapter.checkBoxes.get(i).setEnabled(true);
                                }
                                MainActivity_2.tableAdapter.notifyDataSetChanged();
                            }
                        }
                        System.out.println(selectedStrings.size()+","+selectedStrings.toString());
                    }
                });
            }
        }
        this.setBackgroundColor(Color.BLUE);//背景白色，利用空隙来实现边框
    }
    ArrayList<String> getSelectedString(){
        return selectedStrings;
    }
}